package cn.xopencode.recordservice.service.kvtransformer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.kvtransformer.KvTransformerRecordConvert;
import cn.xopencode.recordservice.convert.kvtransformer.KvTransformerRecordEmpRelConvert;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.kvtransformer.KvTransformerRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.kvtransformer.KvTransformerRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.kvtransformer.KvTransformerRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.kvtransformer.KvTransformerRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.kvtransformer.vo.KvTransformerRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.kvtransformer.vo.KvTransformerRecordPageVO;
import cn.xopencode.recordservice.rpc.kvtransformer.vo.KvTransformerRecordUpdateVO;
import cn.xopencode.recordservice.service.kvtransformer.bo.KvTransformerRecordCreateBO;
import cn.xopencode.recordservice.service.kvtransformer.bo.KvTransformerRecordPageBO;
import cn.xopencode.recordservice.service.kvtransformer.bo.KvTransformerRecordUpdateBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXCEPTION;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_ROBOT_ROOM_NOT_EXITS;

/**
 * 10KV变压器管理service
 */
@Service
@Validated
public class KvTransformerRecordService {

    @Autowired
    private KvTransformerRecordMapper kvTransformerRecordMapper;

    @Autowired
    private KvTransformerRecordEmpRelMapper kvTransformerRecordEmpRelMapper;

    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;


    public PageResult<KvTransformerRecordPageVO> pageKvTransformerRecord(KvTransformerRecordPageBO pageBO) {
        IPage<KvTransformerRecordDO> recordDOIPage = kvTransformerRecordMapper.selectPage(pageBO);
        PageResult<KvTransformerRecordPageVO> recordPageVOPageResult = KvTransformerRecordConvert.INSTANCE.convert(recordDOIPage);
        if (CollectionUtil.isNotEmpty(recordPageVOPageResult.getList())) {
            List<KvTransformerRecordPageVO> list = recordPageVOPageResult.getList();
            for (KvTransformerRecordPageVO vo : list) {
                List<KvTransformerRecordEmpRelDO> empRelDOList = kvTransformerRecordEmpRelMapper.selectListByRecordId(vo.getId());
                List<String> tmp = empRelDOList.stream().map(KvTransformerRecordEmpRelDO::getEmpId).collect(Collectors.toList());
                vo.setEmpIds(String.join(",", tmp));
            }
        }
        return recordPageVOPageResult;
    }

    public Integer createKvTransformerRecord(KvTransformerRecordCreateBO createBO, Set<String> empIds) {
        KvTransformerRecordDO recordDO = KvTransformerRecordConvert.INSTANCE.convert(createBO);
        String recordId = IdUtil.objectId();
        recordDO.setId(recordId);
        Integer result = kvTransformerRecordMapper.insert(recordDO);
        if (!CollectionUtil.isEmpty(empIds)) {
            List<KvTransformerRecordEmpRelDO> kvTransformerRecordEmpRels = empIds.stream().map(empId -> new KvTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordId).setEmpId(empId)).collect(Collectors.toList());
            kvTransformerRecordEmpRelMapper.insertList(kvTransformerRecordEmpRels);
        }
        return result;
    }

    public Integer updateKvTransformerRecord(KvTransformerRecordUpdateBO updateBO, Set<String> empIds) {
        KvTransformerRecordDO recordDO = KvTransformerRecordConvert.INSTANCE.convert(updateBO);
        Integer result = kvTransformerRecordMapper.updateById(recordDO);

        kvTransformerRecordEmpRelMapper.deleteByRecordId(recordDO.getId());
        if (CollectionUtil.isEmpty(empIds)) {
            List<KvTransformerRecordEmpRelDO> kvTransformerRecordEmpRels = empIds.stream()
                    .map(empId -> new KvTransformerRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordDO.getId()).setEmpId(empId)).collect(Collectors.toList());
            kvTransformerRecordEmpRelMapper.insertList(kvTransformerRecordEmpRels);

        }
        return result;
    }

    public KvTransformerRecordUpdateVO getKvTransformerRecordById(String id) {
        KvTransformerRecordDO recordDO = kvTransformerRecordMapper.selectById(id);
        if (recordDO != null) {
            KvTransformerRecordUpdateVO recordVO = KvTransformerRecordConvert.INSTANCE.convertUpdate(recordDO);
            List<KvTransformerRecordEmpRelDO> recordDOList = kvTransformerRecordEmpRelMapper.selectListByRecordId(id);
            recordVO.setEmpList(KvTransformerRecordEmpRelConvert.INSTANCE.convertList(recordDOList));

            PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(recordDO.getRoomId());
            if(roomDO != null) {
                recordVO.setRoomName(roomDO.getRoomName());
            }
            return recordVO;
        }
        return null;
    }


    public Integer dealKvTransformerRecordException(String recordId){
        KvTransformerRecordDO recordDO = kvTransformerRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RECORD_ROBOT_ROOM_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return kvTransformerRecordMapper.updateById(recordDO);
    }


    /**
     * 后台--根据查询条件 分页获取所有的10KV变压器巡检记录
     * @param pageBO
     * @return
     */
    public PageResult<KvTransformerRecordAdminPageVO> getAdminPageKvTransformerRecord(AdminRecordPageBO pageBO){
        Page<KvTransformerRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if(pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat(" 00:00:00"));
            pageBO.setInspectionTime2(date.concat(" 23:59:59"));
        }
        IPage<KvTransformerRecordDO> recordDOList = kvTransformerRecordMapper.adminQuery(page,pageBO);
        return AdminRecordConvert.INSTANCE.convert3(recordDOList);
    }

}
